<?php
if(!session_is_registered("LoginOK"))//bien sessionLoginOK chua ton tai
 	{
 		session_register("LoginOK");
 		$_SESSION['LoginOK'] = 0;
 	}
if(!session_is_registered("UserName"))//bien sessionLoginOK chua ton tai
 	{
 		session_register("UserName");
 		$_SESSION['UserName']="";
 	}
 if(!session_is_registered("Role"))//bien sessionLoginOK chua ton tai
 	{
 		session_register("Role");
 		$_SESSION['Role']=0;
 	}
?>
<!--
XET USERNAME PASSWORD , neu so ko khop ussername pass thi goi trang errormessage truyen tham so loi muon thong bao xem trang errormessage
-->
<?php
include_once("ThreeLayer\Library\Provider.php");
include_once("ThreeLayer\Library\Paging.php");
include_once("ThreeLayer\DTO\dtoUser.php");
include_once("ThreeLayer\DAO\daoUser.php");
include_once("ThreeLayer\BUS\busUser.php");

function redirect($url) { 
if (!headers_sent()) 
header('location: '.$url); 
else { 
echo '<script type="text/javascript">'; 
echo 'window.location.href="'.$url.'";'; 
echo '</script>'; 
echo '<noscript>'; 
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; 
echo '</noscript>'; 
} 
}
?>

<?php
//lay du lieu tu form
if(isset($_GET["fUserName"])){ //nguoi dung da nhap lieu vao form

	$a = busUser::isExist($_GET["fUserName"],$_GET["fUserPass"]);
	
	if($a==true) {

		$user = busUser::getUser($_GET["fUserName"],$_GET["fUserPass"]);
		$role = $user->getBlRole();
		if ($role) $_SESSION['LoginOK']=2;//quyen admin
		else $_SESSION['LoginOK']=1;//quyen nguoi hoc
		$_SESSION['UserName']=$user->getStrFullName();
	}

}

?>
<?php
//CHUYEN HUONG TRANG WEB VE TRANG TUONG UNG VOI QUYEN DANG NHAP
//CHANG HAN: 
//		USER_QUYEN=1(nguoi hoc)---> CHUYEN HUONG VE index.php
//   	USER_QUYEN=2(admin)--->CHUYEN HUONG VE admin.php

if ($_SESSION['LoginOK'] == 1) header("Location: ../index.php");//redirect("../index.php");
if ($_SESSION['LoginOK'] == 2) redirect("../admin.php");	
if ($_SESSION['LoginOK'] == 0) redirect("../errormessage.php?error=1");
?>
